/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lct.eq2.backup;

import com.lct.eq2.multicast.*;
import com.lct.eq2.data.Attack;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Me
 */
public class TestClientThread extends Thread {
    private SocketChannel socket;
    private TestClientGUI parentGui;
    private InetAddress address;
    
    public TestClientThread(SocketChannel socket, TestClientGUI parentGui) {
        this.socket = socket;
        this.parentGui = parentGui;
        this.address = socket.socket().getInetAddress();
    }
    
    public void sendData(Object o) {
        System.out.println("TestClientThread::sendData() - " + o);
        try {
            if (o instanceof String) {
                this.socket.write(ByteBuffer.wrap(((String)o).getBytes()));
            } else {
                ByteArrayOutputStream bStream = new ByteArrayOutputStream();
                ObjectOutputStream oStream = new ObjectOutputStream(bStream);

                oStream.writeObject(o);

                byte[] byteVal = bStream.toByteArray();
                ByteBuffer buff = ByteBuffer.allocate(byteVal.length);
                buff.wrap(byteVal);
                
                buff.compact();
                System.out.println("TestClientThread::sendData() - wrote " + this.socket.write(buff) + " bytes to the socket.");
                buff.flip();
                bStream.close();
                oStream.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void run() {
        ByteBuffer buf = ByteBuffer.allocate(512);
        int bufSize = 0;
        byte[] bytes = null;
        
        while (socket.isConnected()) {
            try {
                buf.clear();
                bufSize = socket.read(buf);

                if (bufSize > 0) {
                    bytes = new byte[bufSize];
                    System.out.println("TestClientThread::Run() - Reading " + bufSize + "b");
                    // Retrieve bytes between the position and limit
                    buf.flip();
                    buf.get(bytes, 0, bytes.length);
                    buf.compact();
                    
                    System.out.println(new String(bytes));
//                    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
//
//                    Object o = ois.readObject();
//                    if (o instanceof String) {
//                        System.out.println(o);
//                    } else if (o instanceof Attack) {
//                        Attack attack = (Attack) o;
//                        System.out.println(attack.getAttackName());
//                    }
                }
                Thread.currentThread().sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (ClassNotFoundException ex) {
//                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                if (ex.getMessage().indexOf("forcibly closed") > 0) {
                    try {
                        this.socket.close();
                    } catch (IOException e) {
                        //nothing
                    }
                }
                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}